package com.jesterplayer;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Environment;
import android.util.Log;

public class PlaybackController {
	private static CurrentTrack ct;
	private static MediaPlayer currentMediaPlayer;
	private static int currentPosition = 0;
	private static boolean repeat;
	private static boolean shuffle = false;
	
	
	private static String musicPath = Environment.getExternalStorageDirectory() + "/music/";
	
	private static ArrayList<String> songs ;
	private static ArrayList<String> shuffledSongs;
	private static ArrayList<String> stored;
	
	public PlaybackController(){
		 songs = new ArrayList<String>();
		 shuffledSongs = new ArrayList<String>();
		 stored = new ArrayList<String>();
		//updateSongList();
		//ct = new CurrentTrack(songs.get(0));
		currentMediaPlayer = new MediaPlayer();
		currentMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		
	}
	
	
	public void play(){
		currentMediaPlayer.start();
	}
	
	
	public void pause(){
		currentMediaPlayer.pause();
	}
	
	public void togglePlayback(){
		if(currentMediaPlayer.isPlaying()){
			currentMediaPlayer.pause();
		}
		else{
			currentMediaPlayer.start();
		}
	}
	
	public void stop(){
		currentMediaPlayer.stop();
		currentMediaPlayer.release();
	}
	
	/*public void start(){
		playIndex(0);
	}*/
	
	public static void playSong(int index) {
		String songPath = songs.get(index);
		try {
			currentMediaPlayer.reset();
			currentMediaPlayer.setDataSource(songPath);
			currentMediaPlayer.prepare();
			currentMediaPlayer.start();

			currentPosition = index;
			
			if (ct != null) {
				ct.setCurrentTrack(songPath);
			} else {
				ct = new CurrentTrack(songPath);
			}
			
			currentMediaPlayer
			.setOnCompletionListener(new OnCompletionListener() {

				public void onCompletion(MediaPlayer arg0) {
					nextSong();
				}
			});

			
		}  catch (IOException e) {
			Log.v("jester", e.getMessage());
		}
	}
	
	public static void playSong(String songPath, int index) {
		try {

			currentMediaPlayer.reset();
			currentMediaPlayer.setDataSource(songPath);
			currentMediaPlayer.prepare();
			currentMediaPlayer.start();

			currentPosition = index;
			
			if (ct != null) {
				ct.setCurrentTrack(songPath);
			} else {
				ct = new CurrentTrack(songPath);
			}

			// Setup listener so next song starts automatically
			currentMediaPlayer
					.setOnCompletionListener(new OnCompletionListener() {

						public void onCompletion(MediaPlayer arg0) {
							nextSong();
						}
					});

		} catch (IOException e) {
			Log.v("jester", e.getMessage());
		}
	}
	
	public static void addToSongList(String file) { 
		PlaybackController.songs.add(file);
		Log.d("MUSIC", file);
	}
	
/*	public  void updateSongList() {
        File home = new File(musicPath);
        
        
        String[] songList = home.list(new Mp3Filter());
        PlaybackController.shuffledSongs = new ArrayList<String>(songList.length);
        
        for (int i = 0; i < songList.length; i++) {  
            PlaybackController.songs.add(songList[i]);
            PlaybackController.shuffledSongs.add(i);
           
        }  
         
		if(PlaybackController.shuffle){
			Collections.shuffle(PlaybackController.shuffledSongs);
		}
		}
        */
	
	class Mp3Filter implements FilenameFilter {
        public boolean accept(File dir, String name) {
                return (name.endsWith(".mp3"));// || name.endsWith(".mp4") || name.endsWith(".flac") || name.endsWith(".wma"));
        }
	}
	
/*	private static void playIndex(int index){

		if (index < songs.size() && index >= 0) {
			playSong(musicPath + songs.get(index));
		} else {
			// Play next song
		}
	}*/
	
	public static void previousSong(){
		if(currentPosition>0){
			playSong(PlaybackController.songs.get(currentPosition-1), currentPosition-1);
		}
	}
	
	public static void nextSong() {
		if (currentPosition != PlaybackController.songs.size()) {
			playSong(PlaybackController.songs.get(currentPosition+1), currentPosition+1);
		}
		
        /*if (currentPosition+1 >= PlaybackController.songs.size()) {
                // Last song, just reset currentPosition
                currentPosition = 0;
        } else {
                playSong(PlaybackController.musicPath + PlaybackController.songs.get(PlaybackController.shuffledSongs.get(PlaybackController.currentPosition+1)));
            if(PlaybackController.currentPosition+1!=PlaybackController.songs.size()){
            	PlaybackController.currentPosition++;
            }
        }*/
	}
	
	public static void toggleShuffle(){
		shuffle=!shuffle;
		if(shuffle){
			shuffledSongs.addAll(songs);
			stored.addAll(songs);
			Collections.shuffle(shuffledSongs);
			songs.clear();
			songs.addAll(shuffledSongs);
		} else {
			songs.clear();
			songs.addAll(stored);
			stored.clear();
		}
	}
}
